class CountDown {
  constructor() {
    this._timer = 0;
    this.time = 0;
    this.isStart = false;
  }
  start(time) {
    this.time = time;
    this.isStart = true;
    this.timerStart();
  }

  pause() {
    clearInterval(this._timer);
  }
  continue() {
    this.timerStart();
  }

  clear() {
    clearInterval(this._timer);
    this.isStart = false;
    this.time = 0;
  }

  timerStart() {
    this._timer = setInterval(() => {
      (this.time && this.time--) || clearInterval(this._timer);
    }, 1000);
  }

  end() {
    const that = this;
    return new Promise((resolve, reject) => {
      const timer = setInterval(() => {
        if (!that.time && that.isStart) {
          resolve("倒计时结束");
          clearInterval(timer);
        }
      }, 10);
    });
  }
}
export { CountDown };
